package com.xncoding.websocket.javax.demo1.Handler;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xncoding.websocket.javax.demo1.model.AbstractMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

public class WsMessageEncoder implements Encoder.Text<AbstractMessage> {
    private static Logger logger = LoggerFactory.getLogger(WsMessageEncoder.class) ;
    @Override
    public void init(EndpointConfig endpointConfig) {
    }
    @Override
    public void destroy() {
    }
    @Override
    public String encode(AbstractMessage tm) throws EncodeException {
        String message = null ;
        try {
            message = new ObjectMapper().writeValueAsString(tm);
        } catch (JsonProcessingException e) {
            logger.error("JSON处理错误：{}", e) ;
        }
        return message;
    }
}